<?php
/**
 * @title 968. 监控二叉树
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param TreeNode $root
     * @return Integer
     */
    function minCameraCover($root) {
        /**
         * @since 2020-09-22 奇数和偶数层节点数量
         */
        $count1 = 0;
        $count2 = 0;

        /**
         * @since 2020-09-22 树的层数和标志
         */
        $level = 0;
        $odd = true;

        /**
         * @since 2020-09-22 层次遍历
         */
        $nexts = [$root];
        while($nexts){
            $nextTmp = [];

            /**
             * @since 2020-09-22 遍历每个节点
             */
            foreach ($nexts as $node){
                /**
                 * @since 2020-09-22 左右节点
                 */
                if($node->left !== null){
                    $nextTmp[] = $node->left;
                } else {}
                if($node->right !== null){
                    $nextTmp[] = $node->right;
                } else {}
            }

            /**
             * @since 2020-09-22 当前层的节点数量
             */
            if($odd){
                $count1 += count($nexts);
            } else {
                $count2 += count($nexts);
            }

            /**
             * @since 2020-09-22 增加层数
             */
            $odd = !$odd;
            $level++;

            /**
             * @since 2020-09-22 下次遍历
             */
            $nexts = $nextTmp;
        }
        // return $level;

        /**
         * @since 2020-09-22 总层数是奇数, 返回偶数层节点数, 反之亦然
         */
        return max(min($count1, $count2), 1);
    }
}

/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.22.php
 */
$datas = [
    // [0,0,null,0,0],
    // [0,0,null,0,null,0,null,null,0],
    [0,null,0,0,0,null,null,null,0],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    // $result = $obj->(...$data);
    $result = $obj->minCameraCover($xhprof->tree($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". $data[1][$i][0] ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
